package com.matt.controller.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.course.Course;
import com.matt.domain.course.CourseChild;
import com.matt.domain.course.CourseType;
import com.matt.domain.courseV2.VendorCoursePackage;
import com.matt.domain.observation.ObservationNewType;
import com.matt.foundation.DO.course.CourseAddDO;
import com.matt.foundation.constant.CourseConstant;
import com.matt.foundation.constant.RequestConstant;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.ClassesQuery;
import com.matt.foundation.query.CourseAreaQuery;
import com.matt.foundation.vo.course.CourseAreaAddVo;
import com.matt.foundation.vo.course.CourseAreaBuildPageVo;
import com.matt.foundation.vo.course.CourseAreaBuildVo;
import com.matt.foundation.vo.course.CourseTypeAddVo;
import com.matt.service.CourseService;
import com.matt.service.CourseTypeService;
import com.matt.service.ObservationNewService;
import com.matt.service.UploadService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/9/1
 * *******************************
 */
@Api(tags = "课程管理")
@RestController
@RequestMapping("/web/course")
public class WebCourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private ObservationNewService observationNewService;
    
    @Autowired
    private CourseTypeService courseTypeService;

    @Autowired
    private SsoCallService ssoCallService;

    @ApiOperation(value = "(新)获取园区所有自建课程类型",httpMethod = "POST")
    @RequestMapping(value = "/get_area_bulid_coursetype")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "header", name = "ticket", value = "票据", required = true, dataType = "string"),
        @ApiImplicitParam(paramType = "query", name = "status", value = "状态", required = false, dataType = "Integer" ,defaultValue = "0"),
    })
    public StatusResponse getAllAreaCourseType(@RequestHeader(RequestConstant.TICKET) String ticket,Integer status) {
        String areaId = cache.getStaff(ticket).getAreaId();
        if (Objects.isNull(status)){
            status =  CourseConstant.CourseOfficialStatus.ACTIVE.codeOf();
        }
        List<CourseType> courseTypeList = courseTypeService.findByAreaIdAndTypeAndStatus(areaId, CourseConstant.Category.AREA_BUILD.codeOf(),  status);
        return StatusResponse.success(courseTypeList);
    }
      
    @ApiOperation(value = "(新)新增园区自建课程类型",httpMethod = "POST")
    @RequestMapping(value = "create_area_build_coursetype")
    public StatusResponse createAreaBuildCourseType(@RequestHeader(RequestConstant.TICKET) String ticket,@RequestBody CourseTypeAddVo courseTypeAddVo) {
    	String areaId = cache.getStaff(ticket).getAreaId();
    	courseTypeAddVo.setAreaId(areaId);
    	CourseType courseType = courseTypeService.create(areaId,courseTypeAddVo);
        return StatusResponse.success(courseType);
    }
    
    @ApiOperation(value = "(新)更新园区自建课程类型状态",httpMethod = "POST")
    @RequestMapping(value = "update_area_build_coursetype")
    public StatusResponse updateAreaBuildCourseType(@RequestHeader(RequestConstant.TICKET) String ticket,@RequestBody CourseTypeAddVo courseTypeAddVo) {
    	String areaId = cache.getStaff(ticket).getAreaId();
    	CourseType courseType = courseTypeService.update(areaId,courseTypeAddVo);
        return StatusResponse.success(courseType);
    }
    
    
    @ApiOperation(value = "所有课程类型",httpMethod = "POST")
    @RequestMapping(value = "/type_name_by_course_type")
    public StatusResponse allType(@RequestHeader(RequestConstant.TICKET) String ticket, String areaId, Integer courseType) {
        StaffDTO staff = cache.getStaff(ticket);
        if (CourseConstant.CourseType.AREA_BUILD.codeOf() == courseType) {
            return StatusResponse.success(courseService.findAreaAllType(areaId));
        }else{
            List<VendorCoursePackage>  result = courseService.findAreaPackages(staff.getAreaId());
            List<Map> typeList = new ArrayList<>();
            result.forEach(o -> {
                Map map = new HashMap();
                map.put("typeCode", o.getId());
                map.put("typeId", o.getId());
                map.put("typeName", o.getName());
                typeList.add(map);
            });
            return StatusResponse.success(typeList);
        }
    }
//
//    @ApiOperation(value = "园区自建课程类型",httpMethod = "POST")
//    @RequestMapping("/area_all_type")
//    public StatusResponse areaAllType(@RequestHeader(RequestConstant.TICKET) String ticket) {
//        StaffDTO staff = cache.getStaff(ticket);
//        List<Map> areaAllType = courseService.findAreaAllType(staff.getAreaId());
//        return StatusResponse.success(areaAllType);
//    }

    @ApiOperation(value = "上传自建课程附件",httpMethod = "POST")
    @RequestMapping(value = "course_area_attach_upload")
    public StatusResponse courseAreaAttachUpload(@RequestHeader(RequestConstant.TICKET) String ticket, MultipartFile file) {
        String url = uploadService.uploadCourseAreaAttachment(file);
        return StatusResponse.success(url);
    }

    @ApiOperation(value = "观测点type列表",httpMethod = "POST")
    @RequestMapping(value = "observation_type_list")
    public StatusResponse courseOfficialChangeStatus() {
        List<ObservationNewType> typeList = observationNewService.findAllType();
        return StatusResponse.success(typeList);
    }
    @ApiOperation(value = "上传教学用具",httpMethod = "POST")
    @RequestMapping(value = "prepare_tool_upload")
    public StatusResponse prepareToolUpload(MultipartFile file) {
        String url = uploadService.uploadPrepareToolImg(file);
        return StatusResponse.success(url);
    }

    @ApiOperation(value = "使用通用模板创建课程",httpMethod = "POST")
    @RequestMapping("/create_course_common")
    public StatusResponse createCourseByCommonTemplete(@RequestHeader String ticket, @RequestBody CourseAddDO courseAddDO) {
        Course course = courseService.createCourseByCommonTemplete(ticket, courseAddDO, null);
        return StatusResponse.success(course);
    }

    @ApiOperation(value = "取课程详细",httpMethod = "POST")
    @RequestMapping("/get_course_common")
    public StatusResponse getCourseCommon(@RequestHeader String ticket , @RequestParam(required = true) String courseId,
                                          @RequestParam(required = true)  Integer type) {
        Course course= courseService.getCourseCommon(courseId,type);
        return StatusResponse.success(course);
    }

    @ApiOperation(value = "取课程列表",httpMethod = "POST")
    @RequestMapping("/list_course_common")
    public StatusResponse getCourseCommonByPage(CourseAreaQuery query) {
        return StatusResponse.success(courseService.getCourseCommonByPage(query));
    }
    
    @ApiOperation(value = "(新)创建或更新园区自建课程",httpMethod = "POST")
    @RequestMapping(value = "createOrUpdate_area_course")
    public StatusResponse createAreaCourse(@RequestHeader String ticket ,@RequestBody CourseAreaAddVo courseAreaAddVo) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        Course course = courseService.createAreaCourse(staff, courseAreaAddVo);

        return StatusResponse.success(course);
    }
    
    @ApiOperation(value = "(新)园区自建课程分页",httpMethod = "POST")
    @RequestMapping(value = "area_bulid_course_page")
    public StatusResponse getAreaBulidCoursePage(@RequestHeader String ticket, CourseAreaQuery query) {
    	String areaId = cache.getStaff(ticket).getAreaId();
    	query.setAreaId(areaId);
    	Page<CourseAreaBuildPageVo> courseAreaBuildPage = courseService.courseAreaBuildPage(query);
        return StatusResponse.success(courseAreaBuildPage);
    }
    
    @ApiOperation(value = "(新)根据课程ID获取园区自建课程明细",httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "courseId", value = "课程ID", required = true, dataType = "string")
    })
    @RequestMapping(value = "get_area_bulid_course_ById")
    public StatusResponse getAreaBulidCourseById(@RequestHeader String ticket, String courseId) {
        String areaId = cache.getStaff(ticket).getAreaId();
    	CourseAreaBuildVo courseAreaBuildVo = courseService.getCourseAreaBuildByCourseId(areaId, courseId);
        return StatusResponse.success(courseAreaBuildVo);
    }
    
    @ApiOperation(value = "(新)根据课程ID删除园区自建课程",httpMethod = "POST")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType="query", name = "courseId", value = "课程ID", required = true, dataType = "string")
    })
    @RequestMapping(value = "delete_area_bulid_course_ById")
    public StatusResponse deleteCourseAreaBuildByCourseId(@RequestHeader String ticket, String courseId) {
        String areaId = cache.getStaff(ticket).getAreaId();
    	boolean bool = courseService.deleteCourseAreaBuildByCourseId(areaId, courseId);
        return StatusResponse.success(bool);
    }

    @ApiOperation(value = "章节管理分页查询",httpMethod = "POST")
    @PostMapping(value = "courseChildList")
    public StatusResponse courseChildList(@RequestBody CourseAreaQuery query) {
        Page<CourseChild> pageList = courseService.courseChildAreaBuildPage(query);
        return StatusResponse.success(pageList);
    }

    @ApiOperation(value = "新增章节",httpMethod = "POST")
    @PostMapping(value = "createCourseChild")
    public StatusResponse createCourseChild(@RequestHeader String ticket, @RequestBody CourseChild courseChild) {
        StaffDTO staff = cache.getStaff(ticket);
        CourseChild courseChild1 = courseService.createCourseChild(staff, courseChild);
        return StatusResponse.success(courseChild1);
    }

    @ApiOperation(value = "章节详情",httpMethod = "GET")
    @GetMapping(value = "courseChildDetail")
    public StatusResponse courseChildDetail(String id) {
        CourseChild courseChild = courseService.courseChildDetail(id);
        return StatusResponse.success(courseChild);
    }

    @ApiOperation(value = "删除章节（包括所有子章节）",httpMethod = "GET")
    @GetMapping(value = "removeCourseChildAll")
    public StatusResponse removeCourseChildAll(String id) {

        return StatusResponse.success(courseService.removeCourseChildAll(id));
    }

//    @ApiOperation(value = "修改章节",httpMethod = "POST")
//    @PostMapping(value = "editCourseChild")
//    public StatusResponse editCourseChild(@RequestHeader String ticket,@RequestBody CourseChild courseChild) {
//        StaffDTO staff = cache.getStaff(ticket);
//        CourseChild courseChild1 = courseService.editCourseChild(staff, courseChild);
//        return StatusResponse.success(courseChild1);
//    }

    @ApiOperation(value = "屏蔽章节",httpMethod = "GET")
    @GetMapping(value = "editStatus")
    public StatusResponse editStatus(String id, Integer status) {

        return StatusResponse.success(courseService.editStatus(id,status));
    }


    @ApiOperation(value = "获取当前章节子章节",httpMethod = "GET")
    @GetMapping(value = "getChild")
    public StatusResponse getChild(String id) {
        List<CourseChild> childList = courseService.getChild(id);
        return StatusResponse.success(childList);
    }

    @ApiOperation(value = "新建课程",httpMethod = "POST")
    @RequestMapping(value = "createCourse")
    public StatusResponse createAreaCourse(@RequestHeader String ticket ,@RequestBody Course course) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        return StatusResponse.success(courseService.createCourse(staff,course));
    }


    @ApiOperation(value = "课程列表",httpMethod = "POST")
    @RequestMapping(value = "courseList")
    public StatusResponse courseList(@RequestHeader String ticket, @RequestBody ClassesQuery query) {
        String areaId = cache.getStaff(ticket).getAreaId();
        query.setAreaId(areaId);
        Page<Course> coursesPage = courseService.courseList(query);
        return StatusResponse.success(coursesPage);
    }

    @ApiOperation(value = "直播录播课程列表",httpMethod = "POST")
    @RequestMapping(value = "courseLiveList")
    public StatusResponse courseLiveList(@RequestHeader String ticket, @RequestBody ClassesQuery query) {
        String areaId = cache.getStaff(ticket).getAreaId();
        query.setAreaId(areaId);
        Page<Course> coursesPage = courseService.courseLiveList(query);
        return StatusResponse.success(coursesPage);
    }


    @ApiOperation(value = "获取课程的所有章节",httpMethod = "POST")
    @RequestMapping(value = "getCourseChildList")
    public StatusResponse courseLiveList(@RequestHeader String ticket, @RequestBody CourseAreaQuery query) {

        List<CourseChild> courseChildList = courseService.getCourseChildList(query);
        return StatusResponse.success(courseChildList);
    }

    @ApiOperation(value = "教师查看所有章节",httpMethod = "POST")
    @RequestMapping(value = "courseListByTea")
    public StatusResponse courseListByTea(@RequestHeader String ticket, @RequestBody CourseAreaQuery query) {
        String areaId = cache.getStaff(ticket).getAreaId();
        query.setAreaId(areaId);
        Page<CourseChild> courseChildList = courseService.courseListByTea(query);
        return StatusResponse.success(courseChildList);
    }

    @ApiOperation(value = "新增播放量",httpMethod = "GET")
    @GetMapping(value = "addPlayCount")
    public StatusResponse courseLiveList(String id) {


        return StatusResponse.success(courseService.addPlayCount(id));
    }

}
